package com.ynjtc.desserts.util;

import com.ynjtc.desserts.common.WrapMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.UUID;

/**
 * @Author: lihua
 * @Date: 2025/9/25 09:05
 * @Version: v1.0.0
 * @Description:
 **/
@Slf4j
public class ImageUtil {

    static  public String imgPath = "/Users/lihua/workspace/java/img/";


    static public String saveImgage(MultipartFile file){
        if( file != null){
            try {
                File dir = new File(imgPath);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                String suffix = "";
                String originalFilename = file.getOriginalFilename();
                if ( originalFilename != null && originalFilename.contains(".")) {
                    suffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
                }
                String fileName = UUID.randomUUID() + "."+ suffix;
                file.transferTo(new File(imgPath + fileName));
                return fileName;
            } catch (Exception e) {
                log.error("upload exception ==> {} " , e.getMessage());
                return  null;
            }
        }else {
            return null;
        }
    }
}
